package st.business.tepth.business.visitor.visitorwork.work.detail;

import com.alibaba.fastjson.TypeReference;

import io.reactivex.disposables.Disposable;
import st.business.tepth.base.OnDataConvertListener;
import st.business.tepth.business.visitor.visitorwork.entity.WorkDetailBean;
import st.business.tepth.config.Url;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-06-05 17:33
 */

public class DetailModel implements DetailContract.Model {

    @Override
    public Disposable getWorkDetail(String token, int repairTaskId,
                                    final OnDataConvertListener<WorkDetailBean> listener) {
        return RxRestClient.builder()
                .url(Url.GetWorkDetail)
                .params("repairTaskId", repairTaskId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        listener.convert(convertToData(response));
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        listener.error(code, msg);
                    }
                })
                .build()
                .post();
    }

    private WorkDetailBean convertToData(String response) {
        return new ObjConverter<WorkDetailBean>().convert(response
                , new TypeReference<WorkDetailBean>() {
                });
    }
}
